Even number squared plus one

An even number squared plus one is either prime or can be divided by a previous (2n)^2+1

Last modified
Lines 111

Parent directory Download CGIread sitemap Main page

Quick links: (none)

  1. /*
  2.  * 
  3.  * Even number squared plus one.
  4.  *
  5.  * An even number squared plus one is either prime or can be divided
  6.  * by a previous (2n)^2+1
  7.  */
  8. #include <stdint.h>
  9. typedef struct prime0_item_s {
  10.     struct prime0_item_s *next;
  11.     uint64_t item;
  12. } prime0_item;
  13. void prime0_init(prime0_item **list, uint32_t *counter);
  14. uint64_t prime0_iterate(prime0_item *list, uint32_t *counter);
  15. /* SPLIT HERE */
  16. #include <stdlib.h>
  17. void prime0_init(prime0_item **list, uint32_t *counter)
  18. {
  19.     *list = malloc(sizeof(prime0_item));
  20.     (*list)->next = NULL;
  21.     (*list)->item = 5;
  22.     *counter = 2;
  23. }
  24. uint64_t prime0_iterate(prime0_item *list, uint32_t *counter)
  25. {
  26.     register uint64_t number, d;
  27.     uint64_t stop;
  28.     char insert_here;
  29.     prime0_item *item, *next;
  30.     /* Step to the next even number */
  31.     *counter += 2;
  32.     /* MUST expand the counter before calculating the next candidate. */
  33.     stop = (uint64_t) *counter;
  34.     number = stop * stop + 1;
  35.     /* Begin factorization */
  36.     for (item = list; item; item = item->next)
  37.     {
  38.         d = item->item;
  39.         if (d >= stop || 1 == number)
  40.             break;
  41.         while (0 == number % d)
  42.             number /= d;
  43.     }
  44.     /* If number isn't one, it must be a new prime. */
  45.     if (1 != number)
  46.     {
  47.         for (item = list; item; item=item->next)
  48.         {
  49.             /* Criteria for correct placement. */
  50.             if (item->next)
  51.                 insert_here = item->next->item > number;
  52.             else
  53.                 insert_here = 1;
  54.             if (insert_here)
  55.             {
  56.                 /* Insert the new prime */
  57.                 next = item->next;
  58.                 item->next = malloc(sizeof(prime0_item));
  59.                 item->next->item = number;
  60.                 item->next->next = next;
  61.                 break;
  62.             }
  63.         }
  64.     }
  65.     return number;
  66. }
  67. /* SPLIT HERE */
  68. #include <stdio.h>
  69. #include <stdlib.h>
  70. int main(int argc, char *argv[])
  71. {
  72.     uint32_t counter, stop;
  73.     prime0_item *list, *item;
  74.     char *ign;
  75.     
  76.     if (argc != 2)
  77.     {
  78.         fputs(
  79.             "Usage: prime0 stop-number\n"
  80.             "\n"
  81.             "This program will print a list of primes that are factors\n"
  82.             "to squared even numbers plus one.  stop-number will be the\n"
  83.             "highest number that will be squared.\n"
  84.             "",
  85.             stderr
  86.         );
  87.         return 1;
  88.     }
  89.     
  90.     stop = strtoul(argv[1], &ign, 0);
  91.     
  92.     prime0_init(&list, &counter);
  93.     
  94.     while (counter <= stop)
  95.         prime0_iterate(list, &counter);
  96.     
  97.     for (item = list; item; item = item->next)
  98.         printf("%llu\n", item->item);
  99. }